'''
Created on 07/12/2009

@author: eh01
'''
from Queue import Queue
import base64
import os
import time
import unittest

from datastore import database
from datastore.resource import Resource
from runtime import rt
from runtime.spaces import Spaces, SpacesContext
from services.httpclienttask import HTTPClientTask


class HTTPClientTaskTest(unittest.TestCase):


    def setUp(self):
        queue = Queue()
        self.spaces = Spaces()
        spaces_context = SpacesContext(self.spaces, queue)
        runtime = rt.Runtime()
        runtime.initialise_spaces_with_local_config_files(spaces_context)

        self.temp_database_name = 'test.db'
        self.original_database_name = database.DATABASE_NAME
        database.DATABASE_NAME = self.temp_database_name        


    def tearDown(self):
        try:
            os.remove(self.temp_database_name)
        except:
            pass
        
    def test_html(self):
        '''test getting html'''
        queue = Queue()
        self.spaces = Spaces()
        spaces_context = SpacesContext(self.spaces, queue)
        runtime = rt.Runtime()
        runtime.initialise_spaces_with_local_config_files(spaces_context)       
        uri_tasks = self.spaces.uri('tasks:')
        uri_httpclient = self.spaces.uri('httpclient:')
        task_def_resource = Resource()
        httpclient_task = HTTPClientTask(self.spaces, uri_tasks.base.services.httpclienttask, task_def_resource)
        httpclient_task.start()
        time.sleep(1)
        res = Resource()
        res.url = 'http://www.google.com/gmail'
        res.http_method = 'GET'
        res.data = None
        request_uri = spaces_context.put(uri_httpclient.request.collection(True), res)
        res = spaces_context.get_wait(request_uri.response)
        httpclient_task.task_stop()
        self.assertTrue(base64.b64decode(res.content))
        self.assertEqual(200, res.code)

    def test_binary(self):
        '''test getting binary'''
        queue = Queue()
        self.spaces = Spaces()
        spaces_context = SpacesContext(self.spaces, queue)
        runtime = rt.Runtime()
        runtime.initialise_spaces_with_local_config_files(spaces_context)       
        uri_tasks = self.spaces.uri('tasks:')
        uri_httpclient = self.spaces.uri('httpclient:')
        task_def_resource = Resource()
        httpclient_task = HTTPClientTask(self.spaces, uri_tasks.base.services.httpclienttask, task_def_resource)
        httpclient_task.start()
        time.sleep(1)
        res = Resource()
        # download a exe
        res.url = 'http://downloads.sourceforge.net/sevenzip/7z465.exe'
        res.http_method = None
        res.data = None
        request_uri = spaces_context.put(uri_httpclient.request.collection(True), res)
        res = spaces_context.get_wait(request_uri.response)
        httpclient_task.task_stop()
        self.assertTrue(base64.b64decode(res.content))
        self.assertEqual(200, res.code)
        
    def test_invalid_url(self):
        '''test invalid url'''
        queue = Queue()
        self.spaces = Spaces()
        spaces_context = SpacesContext(self.spaces, queue)
        runtime = rt.Runtime()
        runtime.initialise_spaces_with_local_config_files(spaces_context)       
        uri_tasks = self.spaces.uri('tasks:')
        uri_httpclient = self.spaces.uri('httpclient:')
        task_def_resource = Resource()
        httpclient_task = HTTPClientTask(self.spaces, uri_tasks.base.services.httpclienttask, task_def_resource)
        httpclient_task.start()
        time.sleep(1)
        res = Resource()
        # download a exe
        res.url = 'http://localhost:1111/'
        res.http_method = None
        res.data = None
        request_uri = spaces_context.put(uri_httpclient.request.collection(True), res)
        res = spaces_context.get_wait(request_uri.response)
        httpclient_task.task_stop()
        self.assertEqual(404, res.code)
        
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()